package com.example.zkfx

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.base.mvvm.BaseViewActivity
import com.example.zkfx.entity.MyTabEntity
import com.example.zkfx.fg.AllFragment
import com.example.zkfx.fg.ChangeFragment
import com.example.zkfx.fg.SignFragment
import com.example.zkfx.databinding.ActivityMainBinding
import com.example.zkfx.room.AppDbUtils
import com.example.zkfx.room.Score
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.android.material.chip.Chip
import java.text.SimpleDateFormat

class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    lateinit var vpAdapter: VpAdapter
    val fraglist = mutableListOf<Fragment>()//存放fragment
    val tabs = arrayListOf<CustomTabEntity>()//存放commontablayout实体类

    lateinit var allFragment: AllFragment
    lateinit var signFragment: SignFragment
    lateinit var changeFragment: ChangeFragment

    //定义一个set用来收集输入的关键词
    val set = mutableSetOf<String>()

    @SuppressLint("SimpleDateFormat")
    override fun initView() {
        //展示流式布局
        //先查询set中的数据
        queryName()


        binding.add.setOnClickListener {
            set.add(binding.et.text.toString())
            binding.et.setText("")
            queryName()
        }




        allFragment = AllFragment()
        signFragment = SignFragment()
        changeFragment = ChangeFragment()
        fraglist.add(allFragment)
        fraglist.add(signFragment)
        fraglist.add(changeFragment)
        vpAdapter = VpAdapter(this,fraglist)
        binding.vp.adapter = vpAdapter

        binding.vp.offscreenPageLimit = 2//将当前fragment左右的各2个fragment进行初始化

        tabs.add(MyTabEntity("全部",0,0))
        tabs.add(MyTabEntity("收入",0,0))
        tabs.add(MyTabEntity("支出",0,0))
        binding.ctl.setTabData(tabs)

        //上下联动
        binding.ctl.setOnTabSelectListener(object:OnTabSelectListener{
            override fun onTabSelect(position: Int) {
               binding.vp.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        binding.vp.registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.ctl.currentTab = position
            }
        })

        //点击签到，插入一条type为0的数据、 //实时更新总数据
        binding.sign.setOnClickListener {
            val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val format = simpleDateFormat.format(System.currentTimeMillis())
            AppDbUtils.getDb().getDao().insert(Score(0,46,0,format))//新增一条数据

            queryAll()
            allFragment.queryAll()
            signFragment.queryAll()
            changeFragment.queryAll()
        }
        queryAll()//一进来先查询
        binding.change.setOnClickListener {
            val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val format = simpleDateFormat.format(System.currentTimeMillis())
            AppDbUtils.getDb().getDao().insert(Score(0,-100,1,format))//新增一条数据

            queryAll()
            allFragment.queryAll()
            signFragment.queryAll()
            changeFragment.queryAll()
        }
    }

    private fun queryName() {
        if(set.size>0){
            binding.cg.removeAllViews()
            set.forEach {
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip, null) as Chip
                chip.findViewById<TextView>(R.id.name).text = it
                binding.cg.addView(chip)
                //点击回显到上方et
                chip.setOnClickListener { it1->
                    binding.et.setText(it)
                }
                //点击删除
                chip.setOnCloseIconClickListener { it2->
                    set.remove(it)
                    binding.cg.removeView(chip)
                }
            }
        }
    }

    private fun queryAll() {
        var total = 0//总合计
        var totalSign = 0//收入
        var totalChange = 0//支出
        val queryAll = AppDbUtils.getDb().getDao().queryAll()
        if(queryAll.isNotEmpty()){
            queryAll.forEach {
                total +=it.num///意味数据存在负数
            }
        }
        binding.total.text = "当前积分：$total 分"
        val querySign = AppDbUtils.getDb().getDao().queryType(0)
        if(querySign.isNotEmpty()){
            querySign.forEach {
                totalSign +=it.num
            }
        }
        binding.totalSign.text = "收入合计:$totalSign 分"

        val queryChange = AppDbUtils.getDb().getDao().queryType(1)
        if(queryChange.isNotEmpty()){
            queryChange.forEach {
                totalChange +=it.num//这个数都是负数
            }
        }
        // 0 -()
        binding.totalChange.text = "支出合计:${0-totalChange} 分"

    }

    override fun initData() {

    }

    override fun getLayout(): Int  = R.layout.activity_main

}